package io.github.junxworks.qt.modules.mm.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import io.github.junxworks.ep.core.orm.TBaseMapper;
import io.github.junxworks.qt.modules.mm.dto.MmVariableCondition;
import io.github.junxworks.qt.modules.mm.entity.MmVariable;
import io.github.junxworks.qt.modules.mm.vo.MmVariableVo;

@Mapper
public interface MmVariableMapper extends TBaseMapper<MmVariable> {

	List<MmVariableVo> queryList(MmVariableCondition condition);

	List<MmVariableVo> queryAllVariablesByVarIds(List<Long> varIds);

	List<Long> queryAllDatastreamIdsByVarIds(List<Long> varIds);
	
	@Select("select * from mm_variable where id=#{id}")
	MmVariableVo queryMmVariableById(@Param("id") Long id);

	@Select("select * from mm_variable where status=0 and name=#{name}")
	MmVariableVo queryMmVariableByName(@Param("name") String name);

	@Select("SELECT group_concat(e.exp_name) `names` FROM mm_exp_var_rela evr, mm_expression e WHERE evr.var_id = #{id} AND evr.exp_id = e.id AND e.status = 0 GROUP BY evr.var_id")
	String queryRelaExpNamesByVarId(Long id);

	@Select("SELECT v.id  FROM mm_var_var_rela vvr, mm_variable v WHERE vvr.var_id_from = #{id} AND vvr.var_id_to = v.id AND v.status = 0")
	List<Long> queryRelaVarIdsByVarId(Long varId);

	@Select("SELECT group_concat(e.name) `names` FROM mm_var_var_rela eer, mm_variable e WHERE eer.var_id_to = #{id} AND eer.var_id_from = e.id AND e.status = 0 GROUP BY eer.var_id_to")
	String queryRelaVarNamesByExpId(Long varId);

	@Delete("delete from mm_var_ds_rela where var_id=#{id}")
	int deleteVarDsRela(@Param("id") Long id);

	@Delete("delete from mm_var_var_rela where var_id_from=#{id}")
	int deleteVarVarRela(@Param("id") Long id);
}